<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标位置-拖拽</title>
    <style>
        #outer{
            width: 150px;
            height: 150px;
            background-color: red;
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="outer">

    </div>

    <script>
        let mdx=0;//鼠标距离div
        let mdy=0;

        let mx = 0;//鼠标距离文档左边
        let my = 0;

        let flag = false;

        let outer = document.getElementById("outer");
        outer.onmousedown=function(event){
            let e = event || window.event;

            mdx = e.clientX - outer.offsetLeft;
            mdy = e.clientY - outer.offsetTop;

            flag = true;
        }

        document.onmousemove=function(event){
            if(flag){
                let e = event || window.event;
                outer.style.left=(e.clientX-mdx)+"px";
                outer.style.top=(e.clientY-mdy)+"px";
            }
        }

        document.onmouseup=function(){
            flag = false;
        }
    </script>
</body>
</html>